// elsePage/pages/sigin/sigin.js
const util = require('../../../utils/util.js')
const app = getApp()

Page({
  data: {
    logs: [],
    max: [],
    date: '',
    list: [],
    free: [],
    userInfo: {},
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    gqImg: `background:url(${getApp().globalData.imgUrls}sxwww.png) ;background-size:100% 100%`,
    noImg: `background:url(${getApp().globalData.imgUrls}sxwnosigin.jpg) ;background-size:100% 100%`,
    siginImg: `background:url(${getApp().globalData.imgUrls}sxwsigin.jpg) ;background-size:100% 100%`
  },
  onLoad() {
    let date = this.GetDateStr()
    this.setData({
      date
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
      })
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
        })
      }
    } else {
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
          })
        }
      })
    }
  },
  GetDateStr() {
    let date = util.getTimeDian()
    let { m, d } = date
    m = m < 10 ? '0' + m : m
    d = d < 10 ? '0' + d : d
    return m + "月" + d;
  },
  getUserInfo(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  dataViewTap(e) {
    let ev = e.currentTarget.dataset,
    is_sign = ev.is_sign, // 0 -> 未签到   1-> 已签到
    date_md = ev.gload.date_md,
    index = ev.index;
    if (is_sign === 0 && this.data.date === date_md) { // 说明点击的事今天
      util.API('pyapi/click_sign', {}, 'application/x-www-form-urlencoded').then(({
        code,
        sum_coupon,
        sum_fee,
        sum_money,
        msg
      }) => {
        if (code == 200) {
          let that = this
          if (sum_coupon){
            wx.showToast({
              title: '获得奖励：优惠券' + sum_coupon ,
              icon: 'none'
            })
          }else{
            wx.showToast({
              title: '获得奖励：' + sum_fee + '积分，' + sum_money + '余额',
              icon: 'none'
            })
          }
          that.data.list[index].is_sign = 1;
          that.setData({
            list: that.data.list
          })
        } else {
          wx.showToast({
            title: msg + '',
            icon: 'none'
          })
        }
      })
    } else {
      if (is_sign === 0) {
        wx.showToast({
          title: '不能提前签到',
          icon: 'none'
        })
      } else if (is_sign === 1) {
        wx.showToast({
          title: '不能重复签到',
          icon: 'none'
        })
      }
    }
  },
  onShow() {
    this.getList()
    this.getData()
  },
  getData() {
    util.API('pyapi/date_of_list', {}, 'application/x-www-form-urlencoded').then(({
      sign_list,
      code,
      msg
    }) => {
      if (code == 200) {
        this.setData({
          list: sign_list
        })
        console.log(sign_list)
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
  getList() { // 页面信息
    util.API('pyapi/datainfo', {}, 'application/x-www-form-urlencoded').then(({
      fee,
      member,
      code,
      msg
    }) => {
      if (code == 200) {
        this.setData({
          max: member[0].max_sign_day, // 已连续签到天数
          logs: fee[0], // 活动规则
        })
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
})
